package com.woniuxy.demo.epai.controller;


import com.alipay.api.AlipayClient;
import com.woniuxy.demo.epai.common.Result;
import com.woniuxy.demo.epai.controller.component.ControllerBase;
import com.woniuxy.demo.epai.controller.form.UserRegisterForm;
import com.woniuxy.demo.epai.controller.form.userForm;
import com.woniuxy.demo.epai.model.Members;
import com.woniuxy.demo.epai.model.User;
import com.woniuxy.demo.epai.service.AliPayService;
import com.woniuxy.demo.epai.service.impl.AliPayServiceImpl;
import com.woniuxy.demo.epai.service.impl.MembersServiceImpl;
import com.woniuxy.demo.epai.service.impl.UserServiceImpl;
import com.woniuxy.demo.epai.vo.Alivo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author epai
 * @since 2021-04-12
 */
@RestController
@RequestMapping("/epai/user")
@Slf4j
public class UserController extends ControllerBase {
    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private MembersServiceImpl membersService;

    @Autowired
    private AliPayServiceImpl aliPayService;

    /**
     * 根据传入的ID查询当前用户的状态
     * @param userId
     * @return
     */
    @GetMapping("/getVipStatus")
    public Result getVipStatus(Integer id){
        log.info("传入userId={}",id);
        User vip = userService.findVip(id);
        //获取当前用户vip状态 0不是Vip 1是Vip
        int vipStatus = userService.getVipStatusById(id);
        if(vipStatus==1){
            return Result.success(vip);
        }else{
            return Result.fail("当前用户不是Vip");
        }
    }

    /**
     * 增加新会员
     * @param user
     * @return
     */
    @PutMapping("/addVip")
    public String addVip(@RequestBody userForm user){
        log.info("传入的userId为={}传入的保证金为{}",user.getId(),user.getGuartantee());
        userService.addVid(user);
        Alivo alivo = new Alivo();
        Random random = new Random();
        Integer i = random.nextInt(10000);
        alivo.setOut_trade_no(i.toString());
        alivo.setSubject(i.toString());
        alivo.setTotal_amount(user.getGuartantee().toString());
        alivo.setDescription("你好世界");
        String pay = aliPayService.aliPay(alivo);
        return pay;
    }

    /**
     * 会员基本信息
     * @param user
     * @return
     */
    @GetMapping("/findVip")
    public Result findVip(Integer id){
        log.info("传入的userId为={}",id);
        User vip = userService.findVip(id);
        return Result.success(vip);
    }

    /**
     * 会员成长
     * @param user
     * @return
     */
    @GetMapping("/levelVip")
    public Result levelVip(int id){
        log.info("传入的userId为={}",id);
        Members members = membersService.getMembers(id);
        if(members==null){
            return Result.fail("查无此人");
        }
        return Result.success(members);
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @GetMapping("/userLogin")
    public Result userLogin(String username,String password){
        log.info("传入的username为={}传入的password为={}",username,password);
        Integer integer = userService.userLogin(username, password);
        //根据查询的用户id返回用户数据
        User user = userService.findVip(integer);
        if(integer!=null){
            return Result.success(user);
        }
        return Result.fail("账号或者密码错误");


    }
    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody UserRegisterForm userRegisterForm){
        log.info("传入的username为{}传入的password为{}",userRegisterForm.getUsername(),userRegisterForm.getPassword());
        userService.userRegister(userRegisterForm);
        return Result.success();
    }
}
